package com.spring.springboot.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.http.ResponseEntity;

import java.util.HashMap;
import java.util.Map;

/**
 * @author admin
 * @date 2022-07-21 17:10:17:10
 * @description
 */
public class ResponseUtil {

    public ResponseUtil() {
    }

    public static ResponseEntity<Map<String, Object>> success(Object data) {
        Map<String, Object> result = buildBase(0, "");
        result.put("data", data);
        return ResponseEntity.ok(result);
    }

    public static ResponseEntity<Map<String, Object>> success(Page<T> pageInfo) {
        Map<String, Object> page = new HashMap<>(16);
        page.put("recordsTotal", pageInfo.getTotal());
        page.put("recordsFiltered", pageInfo.getTotal());
        page.put("data", pageInfo.getRecords());
        return ResponseEntity.ok(page);
    }

    public static ResponseEntity<Map<String, Object>> error(String msg) {
        Map<String, Object> result = buildBase(-1, msg);
        result.put("data", "");
        return ResponseEntity.status(500).body(result);
    }

    public static ResponseEntity<Map<String, Object>> error(int status, String msg) {
        Map<String, Object> result = buildBase(-1, msg);
        result.put("data", "");
        return ResponseEntity.status(status).body(result);
    }

    private static Map<String, Object> buildBase(int code, String msg) {
        Map<String, Object> data = new HashMap<>(16);
        data.put("code", code);
        data.put("msg", msg);
        return data;
    }

    public class Response {
        private Object data;
        private String msg;
        private int code;

        public Response() {
        }

        public Object getData() {
            return this.data;
        }

        public void setData(Object data) {
            this.data = data;
        }

        public String getMsg() {
            return this.msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public int getcode() {
            return this.code;
        }

        public void setcode(int code) {
            this.code = code;
        }
    }
}
